﻿using System;
using System.Collections;
using System.Data;

public partial class Admin_News_QL_HoiDap : System.Web.UI.Page
{
    Data data = new Data();
    int indexOfImg = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["admin"].Equals("true") == false)
            {
                Response.Redirect("../Default.aspx");
            }
            else
            {
                if (IsPostBack == false)
                {
                    FillData();
                }
                else
                {
                    Panel1.Visible = true;
                }
                if (Request.QueryString["delete"] != null)
                {
                    data.Delete_HoiDap(Request.QueryString["delete"]);
                    btnGetLinhVucCon_Click(null, null);
                }

            }
        }
        catch (Exception ex)
        {
            Response.Redirect("../Default.aspx");
            throw;
        }
      
    }

    private void FillData()
    {
        Calendar1.SelectedDate = DateTime.Now;
        DataTable table = data.GetTable_By_Name("MucLucPhapLuat");
        comboboxLinhVucCha.DataSource = table;
        comboboxLinhVucCha.DataTextField = "MucPhapLuatDescription";
        comboboxLinhVucCha.DataValueField = "MucPhapLuatID";
        comboboxLinhVucCha.DataBind();
        Panel1.Visible = false;
    }

    protected void btnGetLinhVucCon_Click(object sender, EventArgs e)
    {
        DataTable table = data.GetTable_By_Where("HoiDap", "where MucPhapLuatID=" + comboboxLinhVucCha.SelectedValue);
        DataListYahoo.DataSource = table;
        DataListYahoo.DataBind();
    }
    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void btnSave_Click1(object sender, EventArgs e)
    {
        String fileanh = "";
        Random random = new Random();
        String noidung = CKEditor1.Text;

        if (FileUpload1.HasFile)
        {
            int _random = random.Next(100000);
            fileanh = FileUpload1.FileName.Split('.')[0] + "_" + _random + "." + FileUpload1.FileName.Split('.')[1] + ";";
            FileUpload1.SaveAs(Server.MapPath("~/Images/HoiDap/" + FileUpload1.FileName.Split('.')[0] + "_" + _random + "." + FileUpload1.FileName.Split('.')[1]));
            if (chk_source.Checked == false)
            {
                noidung = ReplaceImgSrc(noidung, FileUpload1.FileName.Split('.')[0] + "_" + _random, indexOfImg);
            }
        }
        if (FileUpload2.HasFile)
        {
            int _random = random.Next(100000);
            fileanh = fileanh + FileUpload2.FileName.Split('.')[0] + "_" + _random + "." + FileUpload2.FileName.Split('.')[1];
            FileUpload2.SaveAs(Server.MapPath("~/Images/HoiDap/" + FileUpload2.FileName.Split('.')[0] + "_" + _random + "." + FileUpload2.FileName.Split('.')[1]));
            if (chk_source.Checked == false)
            {
                noidung = ReplaceImgSrc(noidung, FileUpload2.FileName.Split('.')[0] + "_" + _random, indexOfImg);
            }
        }
        if (FileUpload3.HasFile)
        {
            int _random = random.Next(100000);
            fileanh = fileanh + FileUpload3.FileName.Split('.')[0] + "_" + _random + "." + FileUpload3.FileName.Split('.')[1] + ";";
            FileUpload3.SaveAs(Server.MapPath("~/Images/HoiDap/" + FileUpload3.FileName.Split('.')[0] + "_" + _random + "." + FileUpload3.FileName.Split('.')[1]));
            if (chk_source.Checked == false)
            {
                noidung = ReplaceImgSrc(noidung, FileUpload3.FileName.Split('.')[0] + "_" + _random, indexOfImg);
            }
        }
        if (FileUpload4.HasFile)
        {
            int _random = random.Next(100000);
            fileanh = fileanh + FileUpload4.FileName.Split('.')[0] + "_" + _random + "." + FileUpload4.FileName.Split('.')[1] + ";";
            FileUpload4.SaveAs(Server.MapPath("~/Images/HoiDap/" + FileUpload4.FileName.Split('.')[0] + "_" + _random + "." + FileUpload4.FileName.Split('.')[1]));
            if (chk_source.Checked == false)
            {
                noidung = ReplaceImgSrc(noidung, FileUpload4.FileName.Split('.')[0] + "_" + _random, indexOfImg);
            }
        }
        if (FileUpload5.HasFile)
        {
            int _random = random.Next(100000);
            fileanh = fileanh + FileUpload5.FileName.Split('.')[0] + "_" + _random + "." + FileUpload5.FileName.Split('.')[1] + ";";
            FileUpload5.SaveAs(Server.MapPath("~/Images/HoiDap/" + FileUpload5.FileName.Split('.')[0] + "_" + _random + "." + FileUpload5.FileName.Split('.')[1]));
            if (chk_source.Checked == false)
            {
                noidung = ReplaceImgSrc(noidung, FileUpload5.FileName.Split('.')[0] + "_" + _random, indexOfImg);
            }
        }
        data.Add_HoiDap(comboboxLinhVucCha.SelectedValue, txtHoTen.Text, txtTieude.Text, noidung, Calendar1.SelectedDate.ToShortDateString());
        Response.Redirect("Default.aspx");
    }
    protected String ReplaceImgSrc(String noidung, String noidungInsert, int _indexOfImg)
    {
        ArrayList imgSrc = new ArrayList();
        int indexOfSrc = noidung.IndexOf("src=", _indexOfImg);
        if (indexOfSrc != -1)
        {
            int indexOfGachcheo = noidung.IndexOf("/>", indexOfSrc);
            int indexofDot = noidung.IndexOf(".", indexOfSrc);
            if (indexofDot < indexOfGachcheo && indexOfGachcheo != -1 && indexofDot != -1)
            {
                int khoangcach = indexofDot - indexOfSrc - 5;
                noidung = noidung.Remove(indexOfSrc + 5, khoangcach);
                noidung = noidung.Insert(indexOfSrc + 5, "../Images/AnhBanTinPhapLuat/" + noidungInsert);
            }
            indexOfImg = indexOfGachcheo;
        }

        return noidung;
    }
}
